using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class SkillTrigger_SelfCustomProperty : SkillTrigger
    {
        protected override ESkillTriggerType triggerType => ESkillTriggerType.SelfCustomProperty;

        private ECustomProperty propertyType;
        private int value;

        protected override void OnInitialize(Skill rSkill, TriggerConfig rConfig)
        {
            base.OnInitialize(rSkill, rConfig);
            if (rConfig.customConfig is SkillTrigger_SelfCustomProperty_ConfigData rConfigData)
            {
                this.propertyType = (ECustomProperty)rConfigData.propertyType;
                this.value = rConfigData.value;
            }
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            this.AddListener<ECustomProperty, string, Unit>(EBattleTriggerType.Battle_SetCustomProperty, this.OnEvent);
        }

        private void OnEvent(ECustomProperty rType, string key, Unit rTarget)
        {
            if (this.propertyType == rType && rTarget.GUID == this.executer.GUID)
            {
                var rCustomComp = rTarget.GetComponent<CustomPropertyComponent>(ETComponentType.CustomPropertyComponent);
                if (rCustomComp != null && this.value > 0 && rCustomComp.GetProperty<int>(rType) < this.value)
                {
                    return;
                }

                var rVirInput = ClassPool.Get<SelectParam>();
                rVirInput.unit = this.skill.executer;
                rVirInput.pos = rVirInput.unit.pos;
                this.OnTrigger(rVirInput);
            }
        }

        public override void OnReset()
        {
            base.OnReset();
            this.propertyType = ECustomProperty.Min;
            this.value = default;
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }

#if UNITY_EDITOR
        public override string ToString()
        {
            return $"自身自定义Int参数[{this.propertyType}]修改 >= {this.value}";
        }
#endif
    }
}